<div id='ajaxLogin'>
  <div class='inner'>
    <div class='fheader'>Please Login..</div>
    <form action='${request.contextPath}/j_spring_security_check'
          method='POST' id='ajaxLoginForm'
          name='ajaxLoginForm'
          class='cssform'>
      <p>
        <label for='j_username'>Login ID</label>
        <input type='text' class='text_' name='j_username' value='' />
      </p>
      <p>
        <label for='j_password'>Password</label>
        <input type='password' class='text_' name='j_password' value='' />
      </p>
      <p>
        <label for='j_password'>Remember me</label>
        <input type='checkbox' class='chk' name='_spring_security_remember_me'>
      </p>
      <p>
        <a href='javascript:void(0)' onclick='authAjax(); return false;'>Login</a>
      </p>
    </form>
  </div>
  <div style='display: none; text-align: left;' id='loginMessage'></div>
</div>

<script type='text/javascript'>

// center the form
$('ajaxLogin').style.left = ((document.viewport.getWidth() - 300) / 2) + 'px';
$('ajaxLogin').style.top = ((document.viewport.getHeight() - 300) / 2) + 'px';

function showLogin() { 
  $('ajaxLogin').style.display = 'block';
}

function onSuccessfulLogin() {
  Element.hide('ajaxLogin');
  Element.hide('loginLink');
}

function authAjax() { 
  Element.update('loginMessage', 'Sending request ...');
  Element.show('loginMessage');

  var form = document.ajaxLoginForm;
  var params = Form.serialize(form) + '&spring-security-redirect=/login/ajaxSuccess';

  Form.disable(form);
  new Ajax.Request(form.action, {
    method: 'POST', postBody: params,
    onSuccess:
      function(response) {
        var responseText = response.responseText || '[]';
        var json = responseText.evalJSON();
        if (json.success) {
          onSuccessfulLogin();
        } else if (json.error) {
          Element.update('loginMessage', "<span class='errorMessage'>" + json.error + '</error>');
          Form.enable(document.ajaxLoginForm);
        } else {
          Element.update('loginMessage', responseText);
          Form.enable(document.ajaxLoginForm);
        }
      }
  });
}
</script>